home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / cPaddle.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-10-08  |  9.1 KB  |  269 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cPaddle"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'Here we will encapsulate all of the code needed for the Paddle
  17. 'Local variables for the properties of the Paddle
  18. Private moPosition As D3DVECTOR 'Current position of the Paddle
  19. Private moVelocity As D3DVECTOR 'Current velocity of the Paddle
  20. Private moLastPosition As D3DVECTOR 'Last position of the Paddle
  21. Private moPaddle As CD3DFrame 'D3D Mesh for the Paddle
  22. Private mlPaddleTime As Long 'Last time the Paddle was updated
  23. Private mlTransparantPaddle As Boolean
  24. Public LastVelocityTick As Long 'Last time the paddle's velocity changed
  25. Public PaddleID As Long
  26.  
  27. 'Position property
  28. Public Property Let Position(oPos As D3DVECTOR)
  29.     moPosition = oPos
  30. End Property
  31.  
  32. Public Property Get Position() As D3DVECTOR
  33.     Position = moPosition
  34. End Property
  35.  
  36. 'Velocity property
  37. Public Property Let Velocity(oVel As D3DVECTOR)
  38.     moVelocity = oVel
  39. End Property
  40.  
  41. Public Property Get Velocity() As D3DVECTOR
  42.     Velocity = moVelocity
  43. End Property
  44.  
  45. 'LastPosition prop
  46. Public Property Let LastPosition(oLastPos As D3DVECTOR)
  47.     moLastPosition = oLastPos
  48. End Property
  49.  
  50. Public Property Get LastPosition() As D3DVECTOR
  51.     LastPosition = moLastPosition
  52. End Property
  53.  
  54. 'Transparent property
  55. Public Property Let Transparent(ByVal fTrans As Boolean)
  56.     Dim oMesh As CD3DMesh, oMaterial As D3DMATERIAL8
  57.     Dim lNumMaterial As Long, lCount As Long
  58.     
  59.     mlTransparantPaddle = fTrans
  60.     'now set the property
  61.     Set oMesh = moPaddle.FindChildObject("paddle", 0)
  62.     lNumMaterial = oMesh.GetMaterialCount
  63.     For lCount = 0 To lNumMaterial - 1
  64.         oMaterial = oMesh.GetMaterial(lCount)
  65.         If fTrans Then
  66.             oMaterial.diffuse.a = 0.5
  67.         Else
  68.             oMaterial.diffuse.a = 1
  69.         End If
  70.         oMesh.SetMaterial lCount, oMaterial
  71.     Next
  72. End Property
  73.  
  74. Public Property Get Transparent() As Boolean
  75.     Transparent = mlTransparantPaddle
  76. End Property
  77.  
  78. 'Methods
  79. Public Sub Init(ByVal sMedia As String, sFile As String)
  80.     Set moPaddle = D3DUtil_LoadFromFile(AddDirSep(sMedia) & sFile, Nothing, Nothing)
  81. End Sub
  82.  
  83. Public Sub UpdateTime()
  84.     mlPaddleTime = timeGetTime
  85. End Sub
  86.  
  87. Public Sub UpdatePosition()
  88.     Dim RealVelocity As D3DVECTOR
  89.     
  90.     'Here we will update the position of the paddle
  91.     'and move it based on the velocity assigned.
  92.     If mlPaddleTime = 0 Then mlPaddleTime = timeGetTime
  93.     'First calculate the 'real' velocity (based on the time)
  94.     RealVelocity.X = ((timeGetTime - mlPaddleTime) / 100) * moVelocity.X
  95.     RealVelocity.Y = ((timeGetTime - mlPaddleTime) / 100) * moVelocity.Y
  96.     RealVelocity.z = ((timeGetTime - mlPaddleTime) / 100) * moVelocity.z
  97.     'Let's save our current position
  98.     moLastPosition = moPosition
  99.         
  100.     moPosition.X = moPosition.X + RealVelocity.X
  101.     moPosition.Y = moPosition.Y + RealVelocity.Y
  102.     moPosition.z = moPosition.z + RealVelocity.z
  103.     
  104.     mlPaddleTime = timeGetTime
  105. End Sub
  106.  
  107. Public Sub Render(dev As Direct3DDevice8)
  108.     Dim matPaddle As D3DMATRIX
  109.     
  110.     D3DXMatrixIdentity matPaddle
  111.     D3DXMatrixTranslation matPaddle, moPosition.X, moPosition.Y, moPosition.z
  112.     moPaddle.SetMatrix matPaddle
  113.     moPaddle.Render dev
  114. End Sub
  115.  
  116. Public Sub CleanupFrame()
  117.     Set moPaddle = Nothing
  118. End Sub
  119.  
  120. Public Sub EnsureReality(oldPos As D3DVECTOR, oPuck As cPuck)
  121.     Dim vecDif As D3DVECTOR, nDistance As Single
  122.     Dim vNewVel As D3DVECTOR, nVel As Single
  123.     Dim fMovePaddle As Boolean
  124.     
  125.     'We do *not* want to go 'inside' the puck, don't let it happen
  126.     D3DXVec3Subtract vecDif, oPuck.Position, moPosition
  127.     nDistance = D3DXVec3Length(vecDif)
  128.     If nDistance < (gnPuckRadius + gnPaddleRadius) Then
  129.         'Ok, we are within the puck, now who should move?  The puck or the paddle?
  130.         With moPosition
  131.             fMovePaddle = False
  132.             If .z < (gnFarWallEdge + (gnPaddleRadius + gnPuckRadius)) Then
  133.                 fMovePaddle = True
  134.             End If
  135.             If .z > (gnNearWallEdge - (gnPaddleRadius + gnPuckRadius)) Then
  136.                 fMovePaddle = True
  137.             End If
  138.             If .X < (gnSideRightWallEdge + (gnPaddleRadius + gnPuckRadius)) Then
  139.                 fMovePaddle = True
  140.             End If
  141.             If .X > (gnSideLeftWallEdge - (gnPaddleRadius + gnPuckRadius)) Then
  142.                 fMovePaddle = True
  143.             End If
  144.         End With
  145.         
  146.         If fMovePaddle Then
  147.             'Move the paddle back out so it's not hitting the puck
  148.             Dim vDir As D3DVECTOR, vScale As D3DVECTOR, vPaddleVel As D3DVECTOR
  149.             Dim vPaddleDif As D3DVECTOR
  150.     
  151.             D3DXVec3Subtract vPaddleDif, oPuck.Position, moPosition
  152.             D3DXVec3Subtract vPaddleVel, oldPos, moPosition
  153.             'Get the direction vector by normalizing the pucks velocity
  154.             D3DXVec3Normalize vDir, vPaddleVel
  155.             'Scale the length of the two vectors, plus a little more.
  156.             D3DXVec3Scale vScale, vDir, D3DXVec3Length(vPaddleDif) '(gnPaddleRadius / 4)
  157.             'Move the paddle to it's new location
  158.             D3DXVec3Add moPosition, oldPos, vScale
  159.         'Else We can ignore the case of the puck needing to move because that will
  160.               'happen in checkcollisions call for the puck
  161.         End If
  162.     End If
  163. End Sub
  164.  
  165. Public Sub DoComputerAI(oPuck As cPuck)
  166.  
  167.     Dim vOldPos As D3DVECTOR
  168.     Dim nTempX As Single, nTempZ As Single
  169.     
  170.     'We'll create a simplistic AI opponent
  171.     vOldPos = moPosition
  172.     'Let's just set the velocity of the paddle
  173.     moLastPosition = moPosition
  174.     With moPosition
  175.         If Abs(oPuck.Position.X > .X) Then
  176.             nTempX = Min(oPuck.Velocity.X, gnComputerMaximumVelocity)
  177.         Else
  178.             nTempX = Min(oPuck.Velocity.X, -gnComputerMaximumVelocity)
  179.         End If
  180.         If Abs(oPuck.Position.z - .z) > Abs(oPuck.LastPosition.z - .z) Then
  181.             nTempZ = gnComputerMaximumVelocity
  182.         Else
  183.             nTempZ = -gnComputerMaximumVelocity
  184.         End If
  185.     End With
  186.     moVelocity = vec3(nTempX, 0, nTempZ)
  187.     'If the puck is in *front* of the paddle, just move the paddle directly backwards
  188.     If moPosition.z < oPuck.Position.z Then
  189.         moVelocity = vec3(0, 0, gnComputerMaximumVelocity)
  190.     End If
  191.     UpdatePosition
  192.     EnsureReality vOldPos, oPuck
  193.     
  194.     With moPosition
  195.         nTempX = .X
  196.         nTempZ = .z
  197.         If PaddleID = 0 Then
  198.             If nTempZ > -(gnPaddleRadius * 1.5) Then
  199.                 nTempZ = -(gnPaddleRadius * 1.5)
  200.             ElseIf nTempZ < (gnFarWallEdge + (gnPaddleRadius)) Then
  201.                 nTempZ = (gnFarWallEdge + (gnPaddleRadius))
  202.             End If
  203.         Else
  204.             If nTempZ > (gnNearWallEdge - (gnPaddleRadius)) Then
  205.                 nTempZ = (gnNearWallEdge - (gnPaddleRadius))
  206.             ElseIf nTempZ < (gnPaddleRadius * 1.5) Then
  207.                 nTempZ = (gnPaddleRadius * 1.5)
  208.             End If
  209.         End If
  210.         If nTempX < (gnSideRightWallEdge + (gnPaddleRadius)) Then
  211.             nTempX = (gnSideRightWallEdge + (gnPaddleRadius))
  212.         End If
  213.         If nTempX > (gnSideLeftWallEdge - (gnPaddleRadius)) Then
  214.             nTempX = (gnSideLeftWallEdge - (gnPaddleRadius))
  215.         End If
  216.         moPosition = vec3(nTempX, moPosition.Y, nTempZ)
  217.     End With
  218. End Sub
  219.  
  220. Public Function FadeMesh(FadeInterval As Single) As Boolean
  221.     Dim lNumMaterial As Long
  222.     Dim lCount As Long
  223.     Dim oMaterial As D3DMATERIAL8
  224.     Dim fDoneFading As Boolean
  225.     Dim oMesh As CD3DMesh
  226.     Dim nInternalInterval As Single
  227.     Static lFadeTime As Long
  228.     
  229.     nInternalInterval = FadeInterval
  230.     If lFadeTime = 0 Then
  231.         lFadeTime = timeGetTime
  232.         Exit Function 'We'll do the fade next render pass
  233.     End If
  234.     nInternalInterval = (((timeGetTime - lFadeTime) / 1000000) * nInternalInterval)
  235.     
  236.     Set oMesh = moPaddle.FindChildObject("paddle", 0)
  237.     fDoneFading = True
  238.     lNumMaterial = oMesh.GetMaterialCount
  239.     For lCount = 0 To lNumMaterial - 1
  240.         oMaterial = oMesh.GetMaterial(lCount)
  241.         If nInternalInterval > 0 And oMaterial.diffuse.a <= 1 Then
  242.             oMaterial.diffuse.a = oMaterial.diffuse.a + nInternalInterval
  243.             fDoneFading = False
  244.         ElseIf nInternalInterval < 0 And oMaterial.diffuse.a >= -1 Then
  245.             oMaterial.diffuse.a = oMaterial.diffuse.a + nInternalInterval
  246.             fDoneFading = False
  247.         End If
  248.         oMesh.SetMaterial lCount, oMaterial
  249.     Next
  250.     FadeMesh = fDoneFading
  251. End Function
  252.  
  253. Private Sub Class_Initialize()
  254.     Set moPaddle = Nothing
  255. End Sub
  256.  
  257. Private Sub Class_Terminate()
  258.     Set moPaddle = Nothing
  259. End Sub
  260.  
  261. Private Function Min(ByVal nVal As Single, nVal2 As Single) As Single
  262.     If Abs(nVal) < Abs(nVal2) Then
  263.         Min = nVal
  264.     Else
  265.         Min = nVal2
  266.     End If
  267. End Function
  268.  
  269.